<?php
/**
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

namespace data\formats;

defined('ANS') or die();

class Embed extends Formats implements iFormats {
	var $format = 'embed';

	public function check ($value) {
		$this->error = array();
		$settings = $this->settings;

		if (!$settings['required'] && !$value['']) {
			return true;
		}

		if (substr($value[''], 0, 1) === '<') {
			return true;
		}

		$Url = new Url_media($this->table, $this->name, $this->languages);

		$value[''] = $Url->fixUrl($value[''], true);

		if (!$Url->checkUrl($value[''], 'url')) {
			return false;
		}

		if (!$this->validate($value)) {
			return false;
		}

		if (!$Url->getInfo($value[''])) {
			$this->error[''] = __('Service url for field "%s" is not available', __($this->name));
			return false;
		}

		return true;
	}

	public function valueDB (\data\Db $Db, $value, $language = '', $id = 0) {
		if (substr($value[''], 0, 1) === '<') {
			return $value;
		}

		$Url = new Url_media($this->table, $this->name, $this->languages);

		$value_url = $Url->fixValue(array('url' => $value['']));

		if (!is_array($value_url) || !$value_url['type']) {
			return $value;
		}

		$Html = new \templates\html\Html;
		$Media = new \templates\html\Media($Html);

		$value_url['info'] = unserialize($value_url['info']);

		return array('' => $Media->media($value_url));
	}

	public function settings ($settings) {
		$this->settings = $this->setSettings($settings, array(
			'' => array(
				'db_type' => 'texto',
				'db_type' => 'text',
			)
		));

		return $this->settings;
	}
}
?>